# -*- coding: utf-8 -*-
# Created by methew at 2018/6/10
from flask import current_app as app
from flask_mail import Message, Mail

mail = Mail()


def send_mail(
        sender=None, recipients=None, subject=None, html=None,
        is_log_msg=True):
    """Send mail using Flask-Mail and log the sent message."""
    if is_log_msg:
        log_msg = 'Email sent by site\n'
        log_msg += 'From: <{0}>\n'.format(sender)
        log_msg += 'To: {0}\n'.format(recipients)
        log_msg += 'Subject: {0}\n'.format(subject)
        log_msg += html

        app.logger.info(log_msg)

    if app.debug:
        return

    msg = Message(
        subject,
        sender=sender,
        recipients=recipients)
    msg.html = html
    mail.send(msg)
    return msg
